package com.xhg.mqtt.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * 配置文件读取
 * @Description
 * @author E.T
 * @date 2018-07-17 19:14
 * @updateby
 * @updatedate
 * @version 1.0
 * @since 1.0
 */
public class PropertiesUtil {

    public static String MQTT_HOST;
   public static String MQTT_CLIENTID;
   public static String MQTT_USER_NAME;
   public static String MQTT_PASSWORD;
   public static String MQTT_TOPICS;
   public static int MQTT_TIMEOUT;
   public static int MQTT_KEEP_ALIVE;
   public static int RESEND_TIMES;
   public static int RESEND_COUNT;
   public static int CACHE_TIME;
   public static int RECONN_TIME;
   public static int RECONN_SLEEP_TIME;
   public static int MQTT_TIME_TO_WAIT;
   public static int MAXINFLIGHT;
   public static boolean CLEAN_SESSION;



   static {
      MQTT_HOST = loadMqttProperties().getProperty("spring.mqtt.url");
      MQTT_CLIENTID = loadMqttProperties().getProperty("spring.mqtt.clientid");
      MQTT_USER_NAME = loadMqttProperties().getProperty("spring.mqtt.username");
      MQTT_PASSWORD = loadMqttProperties().getProperty("spring.mqtt.password");
      MQTT_TOPICS = loadMqttProperties().getProperty("spring.mqtt.topics");
      MQTT_TIMEOUT = Integer.valueOf(loadMqttProperties().getProperty("spring.mqtt.timeout","30"));
      MQTT_KEEP_ALIVE = Integer.valueOf(loadMqttProperties().getProperty("spring.mqtt.keepalive","60"));
      CLEAN_SESSION = Boolean.valueOf(loadMqttProperties().getProperty("spring.mqtt.cleansession","true"));
       RESEND_TIMES = Integer.valueOf(loadMqttProperties().getProperty("mqtt.message.resend.times","10"));
       RESEND_COUNT = Integer.valueOf(loadMqttProperties().getProperty("mqtt.message.resend.count","3"));
       CACHE_TIME = Integer.valueOf(loadMqttProperties().getProperty("mqtt.message.cache.time","600"));
      RECONN_TIME = Integer.valueOf(loadMqttProperties().getProperty("mqtt.reconnect.count","5"));
      RECONN_SLEEP_TIME = Integer.valueOf(loadMqttProperties().getProperty("mqtt.reconnect.sleep.time","300"));
      MQTT_TIME_TO_WAIT = Integer.valueOf(loadMqttProperties().getProperty("mqtt.time.to.wait","500"));
      MAXINFLIGHT = Integer.valueOf(loadMqttProperties().getProperty("mqtt.max.inflight","200"));
   }

   private static Properties loadMqttProperties() {
      InputStream inputstream = PropertiesUtil.class.getResourceAsStream("/application.properties");
      Properties properties = new Properties();
      try {
         properties.load(inputstream);
         return properties;
      } catch (IOException e) {
         throw new RuntimeException(e);
      } finally {
         try {
            if (inputstream != null) {
               inputstream.close();
            }
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
      }
   }

   public static void main(String[] args){
       System.out.println(PropertiesUtil.CLEAN_SESSION);
   }

}